package com.dragee.modules.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dragee.common.R;
import com.dragee.modules.sys.entity.FilesEntity;
import com.dragee.modules.sys.service.FilesService;
import com.dragee.utils.UploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;


/**
 * 用户表
 */
@RestController
@RequestMapping("files")
@Api(tags = "BASE - 文件管理")
public class FilesController {
    @Autowired
    private FilesService filesService;



    @GetMapping("getFileById")
    @ApiOperation("根据Id查文件信息")
    public R getFileById(@RequestParam("id") String id){
        FilesEntity file = filesService.getById(id);
        return R.ok().put("data", file);
    }

    @GetMapping("getFilesByFileId")
    @ApiOperation("根据fileId查文件信息")
    public R getFilesByFileId(@RequestParam("fileId") String fileId){
        List<FilesEntity> files = filesService.list(new QueryWrapper<FilesEntity>()
                .eq("fileId", fileId));
        return R.ok().put("data", files);
    }

    /**
     * 删除
     */
    @GetMapping("delete-by-id")
    @ApiOperation("根据fileId查文件信息")
    public R deleteById(@RequestParam("id") String id){
        FilesEntity file = filesService.getById(id);
        File o = new File(file.getFilePath());
        if (o != null) {
            UploadUtil.deleteFile(o);
        }
        filesService.removeById(id);
        return R.ok();
    }

}
